package com.ibm.csorb.dao.impl;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.ibm.csorb.dao.CsBucketDao;
import com.ibm.csorb.entity.CsBucket;

@Component
@Transactional
public class CsBucketDaoImpl extends HibernateDaoSupport implements CsBucketDao {
	private static final Log log = LogFactory.getLog(CsBucketDaoImpl.class);
	// property constants
	public static final String BUCKET_NAME = "bucketName";
	public static final String BUCKET_CLASS = "bucketClass";
	public static final String LOC_IN_CMVC = "locInCmvc";
	public static final String NUM_TESTCASES = "numTestcases";
	public static final String OWNER = "owner";
	public static final String BUCKET_TYPE = "bucketType";
	public static final String RUN_TYPE = "runType";
	public static final String JCC_VERSION = "jccVersion";
	public static final String DB2_VERSION = "db2Version";
	public static final String JCC_DRIVER = "jccDriver";
	public static final String CUSTOMIZE = "customize";
	public static final String ONLINE_CHECK = "onlineCheck";
	public static final String LINE_ITEM_DCR = "lineItemDcr";
	public static final String DESCRIPTION = "description";
	public static final String JCLS_NEEDED = "jclsNeeded";
	public static final String INPUT_FILE_LOC = "inputFileLoc";
	public static final String TEST_FOCUS = "testFocus";
	public static final String OTHER_INSTRUCT = "otherInstruct";
	public static final String ACTIVE_STATUS = "activeStatus";
	public static final String CREATOR = "creator";
	public static final String UPDATOR = "updator";
	public static final String ACTIVATOR = "activator";
	public static final String PRE_BUCKET_ID = "preBucketId";

	protected void initDao() {
		// do nothing
	}

	public void save(CsBucket transientInstance) {
		log.debug("saving CsBucket instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(CsBucket persistentInstance) {
		log.debug("deleting CsBucket instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public CsBucket findById(java.lang.Integer id) {
		log.debug("getting CsBucket instance with id: " + id);
		try {
			CsBucket instance = (CsBucket) getHibernateTemplate().get(
					"com.ibm.csorb.entity.CsBucket", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<CsBucket> findByExample(CsBucket instance) {
		log.debug("finding CsBucket instance by example");
		try {
			List<CsBucket> results = getHibernateTemplate().findByExample(
					instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<CsBucket> findByProperty(String propertyName, Object value) {
		log.debug("finding CsBucket instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from CsBucket as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<CsBucket> findByBucketName(Object bucketName) {
		return findByProperty(BUCKET_NAME, bucketName);
	}

	public List<CsBucket> findByBucketClass(Object bucketClass) {
		return findByProperty(BUCKET_CLASS, bucketClass);
	}

	public List<CsBucket> findByLocInCmvc(Object locInCmvc) {
		return findByProperty(LOC_IN_CMVC, locInCmvc);
	}

	public List<CsBucket> findByNumTestcases(Object numTestcases) {
		return findByProperty(NUM_TESTCASES, numTestcases);
	}

	public List<CsBucket> findByOwner(Object owner) {
		return findByProperty(OWNER, owner);
	}

	public List<CsBucket> findByBucketType(Object bucketType) {
		return findByProperty(BUCKET_TYPE, bucketType);
	}

	public List<CsBucket> findByRunType(Object runType) {
		return findByProperty(RUN_TYPE, runType);
	}

	public List<CsBucket> findByJccVersion(Object jccVersion) {
		return findByProperty(JCC_VERSION, jccVersion);
	}

	public List<CsBucket> findByDb2Version(Object db2Version) {
		return findByProperty(DB2_VERSION, db2Version);
	}

	public List<CsBucket> findByJccDriver(Object jccDriver) {
		return findByProperty(JCC_DRIVER, jccDriver);
	}

	public List<CsBucket> findByCustomize(Object customize) {
		return findByProperty(CUSTOMIZE, customize);
	}

	public List<CsBucket> findByOnlineCheck(Object onlineCheck) {
		return findByProperty(ONLINE_CHECK, onlineCheck);
	}

	public List<CsBucket> findByLineItemDcr(Object lineItemDcr) {
		return findByProperty(LINE_ITEM_DCR, lineItemDcr);
	}

	public List<CsBucket> findByDescription(Object description) {
		return findByProperty(DESCRIPTION, description);
	}

	public List<CsBucket> findByJclsNeeded(Object jclsNeeded) {
		return findByProperty(JCLS_NEEDED, jclsNeeded);
	}

	public List<CsBucket> findByInputFileLoc(Object inputFileLoc) {
		return findByProperty(INPUT_FILE_LOC, inputFileLoc);
	}

	public List<CsBucket> findByTestFocus(Object testFocus) {
		return findByProperty(TEST_FOCUS, testFocus);
	}

	public List<CsBucket> findByOtherInstruct(Object otherInstruct) {
		return findByProperty(OTHER_INSTRUCT, otherInstruct);
	}

	public List<CsBucket> findByActiveStatus(Object activeStatus) {
		return findByProperty(ACTIVE_STATUS, activeStatus);
	}

	public List<CsBucket> findByCreator(Object creator) {
		return findByProperty(CREATOR, creator);
	}

	public List<CsBucket> findByUpdator(Object updator) {
		return findByProperty(UPDATOR, updator);
	}

	public List<CsBucket> findByActivator(Object activator) {
		return findByProperty(ACTIVATOR, activator);
	}

	public List<CsBucket> findByPreBucketId(Object preBucketId) {
		return findByProperty(PRE_BUCKET_ID, preBucketId);
	}

	@SuppressWarnings("unchecked")
	public List<CsBucket> findAll() {
		log.debug("finding all CsBucket instances");
		try {
			String queryString = "from CsBucket";
			System.out.println("in dao query....");
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public CsBucket merge(CsBucket detachedInstance) {
		log.debug("merging CsBucket instance");
		try {
			CsBucket result = (CsBucket) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(CsBucket instance) {
		log.debug("attaching dirty CsBucket instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(CsBucket instance) {
		log.debug("attaching clean CsBucket instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

}